install.packages("tidyverse") ## instalacja pakietu
trying URL 'https://cran.rstudio.com/bin/macosx/big-sur-x86_64/contrib/4.3/tidyverse_2.0.0.tgz'
Content type 'application/x-gzip' length 428408 bytes (418 KB)
==================================================
downloaded 418 KB
The downloaded binary packages are in
/var/folders/zs/2z4rlxv54pv7ml4jld2kccmc0000gn/T//RtmpWuGTkU/downloaded_packages
library(tidyverse)
── Attaching core tidyverse packages ─────────────────────────────────────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.3 ✔ readr 2.1.4
✔ forcats 1.0.0 ✔ stringr 1.5.0
✔ ggplot2 3.4.4 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.0
✔ purrr 1.0.2 ── Conflicts ───────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
library(readxl)
rent <- read_excel(path = "../data-raw/rent-poznan.xlsx")
rent <- as.data.frame(rent)
head(rent)
Wybieramy kolumny z funkcją select z pakietu dplyr.
rent |> select(id, price, flat_area)
rent |> select(1, 5, 10)
rent |> select(1, 5, price)
rent |> select(cena=price, pow=flat_area)
rent |> select(starts_with("p"), ends_with("id"), flat_for_students)
rent |> select(price:flat_for_students, -flat_rooms, -flat_build_year)
rent |> select(contains("ri"))
rent |> select_if(is.character)
rent |> select(contains("ad"))
rent |> select(flat_heating:flat_balcony, -ad_promo)
Wybieramy wiersze z funkcją filter
rent |> filter(price < 500)
rent |> filter(price < 500 & flat_rooms == 1)
rent |> filter(price < 500, flat_rooms == 1)
rent |> filter(price > 5000 | price < 200)
rent |> filter(between(price, 1000, 1200)) ## to i to poniżej jest tym samym
rent |> filter(price >= 1000, price <= 1200)
rent |> filter(price < mean(price))
rent |> filter(price < mean(price), .by = individual)
Zadania: wybranie wierszy po dzielcy: Rataje lub Winogrady
rent |> filter(quarter %in% c("Rataje", "Winogrady"))
Wstawianie nowych kolumn z funkcją mutate (transmute)
rent |> mutate(pricem2 = price/flat_area,
pricem2_log = log(pricem2))
rent |> mutate(pricem2_gr = mean(price), .by = quarter)
rent |> mutate_at(vars(price, flat_area), log)
rent |> mutate_if(is.logical, as.numeric)